package top.dingwen.io.treasure.api.autoconfigure;

import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.dingwen.io.treasure.api.base.constant.ApiConstant;
import top.dingwen.io.treasure.api.client.DefaultClientService;
import top.dingwen.io.treasure.api.dict.DictDefaultConvertor;
import top.dingwen.io.treasure.api.dict.IDictConvertor;
import top.dingwen.io.treasure.api.file.DefaultFileOperator;
import top.dingwen.io.treasure.api.file.IFileOperator;
import top.dingwen.io.treasure.api.upc.*;

import javax.annotation.PostConstruct;

/**
 * 基础场景自动配置类
 *
 * @author dingwen
 * @since 2023/3/5 09:59
 */
@Slf4j
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(ApiProperties.class)
public class ApiAutoconfiguration {


    /**
     * 初始化
     */
    @PostConstruct
    public void init() {
        if (log.isInfoEnabled()) {
            log.info("{} \n\n" +
                            "              _ \n" +
                            "             (_)\n" +
                            "   __ _ _ __  _ \n" +
                            "  / _` | '_ \\| |\n" +
                            " | (_| | |_) | |\n" +
                            "  \\__,_| .__/|_|\n" +
                            "       | |      \n" +
                            "       |_|      " +
                            "                       ",
                    ApiConstant.API_LOG_P_S);
            String properties = JSONUtil.toJsonPrettyStr(SpringUtil.getBean(ApiProperties.class));
            log.info("{},读取到的配置如下:\n{}", ApiConstant.API_LOG_P_S, properties);
            log.info("{},启动成功", ApiConstant.API_LOG_P_S);
        }
    }

    /**
     * 字典转换默认实现
     *
     * @return {@link IDictConvertor }
     */
    @ConditionalOnMissingBean(IDictConvertor.class)
    @Bean
    public IDictConvertor dicDefaultConvertor() {
        log.info("{},{},已成功加载", ApiConstant.API_LOG_P_S, ApiConstant.API_LOG_P_DIC_C);
        return new DictDefaultConvertor();
    }


    /**
     * 默认文件操作对象
     *
     * @return {@link IFileOperator } 文件操作
     */
    @ConditionalOnMissingBean(IFileOperator.class)
    @Bean
    public IFileOperator defaultFileOperator() {
        log.info("{},{},已成功加载", ApiConstant.API_LOG_P_S, ApiConstant.API_LOG_P_DE_FILE);
        return new DefaultFileOperator();
    }

    /**
     * 当前用户获取默认实现
     *
     * @return {@link ICurrentUserService }
     */
    @ConditionalOnMissingBean(ICurrentUserService.class)
    @Bean
    public ICurrentUserService currentUserServiceDefaultImpl() {
        log.info("{},{},已成功加载", ApiConstant.API_LOG_P_S, ApiConstant.API_LOG_P_CUS);
        return new DefaultCurrentUserServiceImpl();
    }

    /**
     * 默认加载用户组件
     *
     * @return {@link ILoadUser }
     */
    @Bean
    @ConditionalOnMissingBean(ILoadUser.class)
    public ILoadUser defaultLoadUserImpl() {
        log.info("{},{},已成功加载", ApiConstant.API_LOG_P_S, ApiConstant.API_LOG_P_DE_LU);
        return new DefaultLoadUserImpl();
    }

    /**
     * 加载用户工厂组件
     *
     * @return {@link LoadUserFactory }
     */
    @Bean
    @ConditionalOnMissingBean(LoadUserFactory.class)
    public LoadUserFactory loadUserFactory() {
        log.info("{},{},已成功加载", ApiConstant.API_LOG_P_S, ApiConstant.API_LOG_P_LU_FA);
        return new LoadUserFactory();
    }

    /**
     * 默认客户端服务实现
     * @return {@link DefaultClientService }
     */
    @Bean
    @ConditionalOnMissingBean(DefaultClientService.class)
    public DefaultClientService defaultClientService() {
        log.info("{},{},已成功加载", ApiConstant.API_LOG_P_S, ApiConstant.API_LOG_P_DCS);
        return new DefaultClientService();
    }


}
